<?php

/**
 * @Author:             wuchenge
 * @Date:               2025-03-04 15:22:33
 * @Last Modified by:   wuchenge
 */
namespace App\Http\Middleware;

use App\Http\Controllers\ApiController;
use App\Libraries\JwtToken;
use App\Models\User;
use Closure;

class UserAuth extends ApiController
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        // 拿到用户
        $user = JwtToken::authToken(User::class);
        if (!$user) {
            return $this->setStatusCode(10401, '请登录')->response();
        }

        $mid_params = ['user' => $user];
        //添加参数
        $request->attributes->add($mid_params);
        return $next($request);
    }
}
